package org.example.treelist;

import com.alibaba.fastjson.JSONArray;
import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

@Data
@ToString
public class Treelist {
    private Integer id;
    private Integer parentId;
    private List<Treelist> children;

    public Treelist(Integer id, Integer parentId) {
        this.id = id;
        this.parentId = parentId;
    }

    /**
     * 递归生成树形list
     * @param allData
     * @param rootId
     * @return
     */
    public static List<Treelist> getTreeList(List<Treelist> allData, Integer rootId) {
        List<Treelist> rootList = allData.stream().filter(e -> rootId == e.getParentId()).collect(Collectors.toList());
        for (Treelist treelist : rootList) {
            List<Treelist> rootChild = allData.stream().filter(e -> e.getParentId() == treelist.getId()).collect(Collectors.toList());
            treelist.setChildren(rootChild);
            rootChild.forEach(e -> e.setChildren(getTreeList(allData, e.getId())));
        }
        return rootList;
    }

    public static List<Treelist> getChildPerms(List<Treelist> list, int parentId) {
        List<Treelist> returnList = new ArrayList<>();
        for (Iterator<Treelist> iterator = list.iterator(); iterator.hasNext(); ) {
            Treelist t = iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    public static void recursionFn(List<Treelist> list, Treelist t) {
        // 得到子节点列表
        List<Treelist> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Treelist tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    public static List<Treelist> getChildList(List<Treelist> list, Treelist t) {
        List<Treelist> tlist = new ArrayList<>();
        Iterator<Treelist> it = list.iterator();
        while (it.hasNext()) {
            Treelist n = it.next();
            if (n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private static boolean hasChild(List<Treelist> list, Treelist t) {
        return getChildList(list, t).size() > 0;
    }

    public static void main(String[] args) {
        List<Treelist> treelists = List.of(new Treelist(1, 0),
                new Treelist(2, 0),
                new Treelist(3, 0),
                new Treelist(4, 1),
                new Treelist(5, 1),
                new Treelist(6, 4),
                new Treelist(7, 4),
                new Treelist(8, 4),
                new Treelist(9, 2),
                new Treelist(10, 2),
                new Treelist(11, 2),
                new Treelist(12, 10),
                new Treelist(13, 3),
                new Treelist(14, 7),
                new Treelist(15, 5),
                new Treelist(16, 5)
        );
        List<Treelist> treeList = getTreeList(treelists, 0);
        System.out.println(JSONArray.toJSONString(treeList));
        System.out.println(JSONArray.toJSONString(getChildPerms(treelists, 0)));
    }
}
